<?php

use App\Utils\Helper\GetterHelper;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

if (!function_exists('exception')) {
    /**
     * 抛出异常
     * @param string $message
     * @param int $code
     * @throws Exception
     */
    function exception ($message = '系统异常', $code = 500) {
        throw new Exception($message, $code);
    }
}
if (!function_exists('access_denied_exception')) {
    /**
     * 禁止访问异常
     * @param string $message
     */
    function access_denied_exception ($message = 'Access Denied.') {
        throw new AccessDeniedException($message);
    }
}
if (!function_exists('not_found_exception')) {
    /**
     * 未找到异常
     * @param string $message
     */
    function not_found_exception ($message = 'Not Found.') {
        throw new NotFoundHttpException($message);
    }
}
if (!function_exists('json_success')) {
    /**
     * json success response
     * @param string $data
     * @return JsonResponse
     */
    function json_success ($data = []) {
        $json = [
            'result'  => [
                'success' => true,
                'code'    => 200,
                'message' => '操作成功',
                'version' => GetterHelper::getParameter('frontend.version'),
            ],
            'content' => $data,
        ];
        return new JsonResponse($json);
    }
}
if (!function_exists('json_failure')) {
    /**
     * json failure response
     * @param string $message
     * @param int $code
     * @return JsonResponse
     */
    function json_failure ($message = '系统异常', $code = 500) {
        $json = [
            'result'  => [
                'success' => false,
                'code'    => $code,
                'message' => $message,
                'version' => GetterHelper::getParameter('frontend.version'),
            ],
            'content' => [],
        ];
        return new JsonResponse($json);
    }
}
if (!function_exists('is_prod_env')) {
    /**
     * 是否生成环境
     * @return bool
     */
    function is_prod_env () {
        return $_ENV['APP_ENV'] == 'prod';
    }
}